<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch"
             label-width="68px" style="display: flex; justify-content: flex-end;">
      <el-form-item label="类型备注" prop="roomRemark">
        <el-input
          v-model="queryParams.roomRemark"
          placeholder="请输入类型备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          style="display: flex; justify-content: flex-end;"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="roomPriceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房间类型" align="center" prop="roomType" width="100px"/>
      <el-table-column label="房间定价" align="center" prop="roomPrice" width="100px"/>
      <el-table-column label="类型备注" align="center" prop="roomRemark" width="200px"/>
      <el-table-column label="护理内容" align="center" prop="nurseContent" width="300px"/>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公寓房间价格对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="房间类型" prop="roomType">
          <el-select v-model="form.roomType" clearable placeholder="请选择">
            <el-option
              v-for="item in dictList"
              :key="item.Id"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房间定价" prop="roomPrice">
          <el-input v-model="form.roomPrice" placeholder="请输入房间定价" />
        </el-form-item>
        <el-form-item label="类型备注" prop="roomRemark">
          <el-input v-model="form.roomRemark" placeholder="请输入类型备注" />
        </el-form-item>
        <el-form-item label="护理内容">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="form.nurseContent">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getRoomPrice ,updateRoomPrice,addRoomPrice} from "@/api/business/roomInfo";
  import {getApartmentDictBySuperCode, listApartmentDict} from "@/api/business/apartmentDict";

  export default {
    name: "RoomPrice",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 公寓房间价格表格数据
        roomPriceList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          roomType: null,
          roomRemark: null,
          roomPrice: null,
          nurseContent: null,
          applyStatus: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        dictList:{},
        user:{},
        queryDict:{
          superCode:1
        }
      };
    },
    created() {
      this.getList();
      this.getDictByCode();
      this.user = this.$store.state.user;
    },
    methods: {
      /** 查询公寓房间价格列表 */
      getList() {
        this.loading = true;
        getRoomPrice(this.queryParams).then(response => {
          this.roomPriceList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          roomPriceId: null,
          roomType: null,
          roomRemark: null,
          roomPrice: null,
          nurseContent: null,
          applyStatus: null,
          createUserId: null,
          createTime: null,
          updateUserId: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.roomPriceId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加公寓房间价格";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.form = row;
        this.open = true;
        this.title = "修改公寓房间价格";
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.roomPriceId != null) {
              this.form.createUserId = this.user.id;
              updateRoomPrice(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              this.form.updateUserId = this.user.id;
              addRoomPrice(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      getDictByCode(){
        //房间类型superCode=1
        getApartmentDictBySuperCode(this.queryDict).then(response =>{
          this.dictList = response.rows;
          console.log(this.dictList);
        })
      },
      computed: {
        isDisabled() {
          return !this.input; // 如果input有值，则禁用，否则不禁用
        },
      },

    }
  };
</script>
